#include "token.h"
#include <stdio.h>





struct token *CreateToken(int code, char* text, int lineno, char* filename, int isstringorint)
{
	int intval = 0;
	char strval[1023];
	char* temp;
	int i = 0;
	struct token* temptok;
	temptok = malloc(sizeof(struct token));
	yytoken = malloc(sizeof(struct token));

	// copy yytext to a temp string
	temp = strdup(yytext);

	// if it's a string or an int, sets up strval
	if(isstringorint > 0)
	{
		strcpy(strval, text);
		//create ival by using atoi on stringval
		intval = atoi(strval);
	}
	else
	{
		strval[0] = ' ';
		strval[1] = '\0';
	}

	//init all the data

	yytoken->category = code;
	yytoken->text = strdup(text);
	yytoken->lineno = lineno;
	yytoken->filename = strdup(filename);
	yytoken->ival = intval;
	yytoken->sval = strdup(strval);

	return yytoken;

}

struct token *DupToken()
{
	struct token *newtoken = malloc(sizeof(struct token));
		//allocate space for token

	// copy everything from yytoken
	newtoken->category = yytoken->category;
	newtoken->text = strdup(yytoken->text);
	newtoken->lineno = yytoken->lineno;
	newtoken->filename = strdup(yytoken->filename);
	newtoken->ival = yytoken->ival;
	newtoken->sval = strdup(yytoken->sval);

	return newtoken;
		// return the new tok
}
